#pragma once
#include <pthread.h>

namespace mutexdouble
{
    class Mutex
    {
    public:
        Mutex()
        {
            pthread_mutex_init(&_mutex, nullptr);
        }

        void Lock()
        {
            pthread_mutex_lock(&_mutex);
        }

        void Unlock()
        {
            pthread_mutex_unlock(&_mutex);
        }

        ~Mutex()
        {
            pthread_mutex_destroy(&_mutex);
        }

    private:
        pthread_mutex_t _mutex;
    };

    /* RAII风格 */
    class MutexGuard
    {
    public:
        MutexGuard(Mutex &lock)
        :_lock(lock) 
        {
            _lock.Lock();
        }
        ~MutexGuard() 
        {
            _lock.Unlock();
        }
    private:
        Mutex &_lock;
    };
}